package com.distribute.distributelock.common.lock;

import redis.clients.jedis.Jedis;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;

/**
 * @description:
 * @author: 刘帅
 * @time: 2020/5/14 9:27
 */
public class LockCase1 extends RedisLock{

    public LockCase1(Jedis jedis, String lockKey) {
        super(jedis, lockKey);
    }

    @Override
    public void sleepBySecond(int second) {
        super.sleepBySecond(second);
    }

    /**
     *@描述
     *@创建人 liushuai
     *@创建时间 2020/5/14 12:30
        * @param null
     *@返回值
     * * 存在问题 没有设置 锁的超时时间
     *  * 如果持有锁的客户端挂了， 则进入死锁
     */
    @Override
    public void lock() {
        while (true) {
            String result = jedis.set(lockKey, "value", "NX");
            if ("OK".equals(result)) {
                System.out.println(Thread.currentThread().getId() + "加锁成功");
                break;
            }
        }
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {
        super.lockInterruptibly();
    }

    @Override
    public boolean tryLock() {
        return super.tryLock();
    }

    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return super.tryLock(time, unit);
    }

    @Override
    public void unlock() {
        jedis.del(lockKey);
    }

    @Override
    public Condition newCondition() {
        return super.newCondition();
    }
}
